package com.xnyzc.lhy.resource.service.impl.oss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.mq.OaCOrderPersonal;
import com.xnyzc.lhy.resource.entity.oss.OaOssFile;
import com.xnyzc.lhy.resource.mapper.oacorder.OaCOrderPersonalMapper;
import com.xnyzc.lhy.resource.mapper.oss.OaOssFileMapper;
import com.xnyzc.lhy.resource.service.oss.IOaOssFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 阿里云oss上传文件对照表 服务实现类
 * </p>
 *
 * @author liuju
 * @since 2019-08-05
 */
@Service
public class OaOssFileServiceImpl extends ServiceImpl<OaOssFileMapper, OaOssFile> implements IOaOssFileService {

    @Autowired
    OaOssFileMapper oaOssFileMapper;
    @Autowired
    OaCOrderPersonalMapper oaCOrderPersonalMapper;

    /**
     * 保存文件信息并获取ID
     *
     * @param fileName
     * @param extendName
     * @param saveFilePath
     * @return
     */
    @Override
    public Long getFileId(String fileName, String extendName, String saveFilePath) {

        OaOssFile oaOssFile = new OaOssFile();
        Long id = IDUtil.nextId();
        Date date = new Date();

        oaOssFile.setOssFileId(id.toString());
        oaOssFile.setFileName(fileName);
        oaOssFile.setExtendName(extendName);
        oaOssFile.setFilePath(saveFilePath);
        oaOssFile.setCreateTime(date);
        oaOssFile.setCreateUser(id);
        oaOssFile.setUpdateTime(date);
        oaOssFile.setUpdateUser(id);

        if (!save(oaOssFile)) {
            throw PangException.create(EErrorCode.noData);
        }
        return id;
    }

    @Override
    public Boolean saveOaOssFile(OaOssFile oaOssFile) {
        if (!save(oaOssFile)) {
            throw PangException.create(EErrorCode.noData);
        }
        return true;
    }

    @Override
    public List<OaOssFile> getFileUrl(String fileIds) {

        String[] ids = fileIds.split(",");

        if (CheckUtil.arrayIsEmpty(ids)) {
            throw PangException.create(EErrorCode.noData);
        }
        QueryWrapper<OaOssFile> oaOssFileQueryWrapper=new QueryWrapper<>();
        oaOssFileQueryWrapper.in(OaOssFile.OSS_FILE_ID,ids);
        List<OaOssFile> oaOssFiles =oaOssFileMapper.selectList(oaOssFileQueryWrapper);
        if(CheckUtil.objIsEmpty(oaOssFiles)){
            throw PangException.create(EErrorCode.unData);
        }

        return oaOssFiles;
    }

    @Override
    public int saveOaCOrderPersonal(String orderNo, String fileId) {
        QueryWrapper<OaCOrderPersonal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaCOrderPersonal.ORDER_NO,orderNo);
        OaCOrderPersonal oaCOrderPersonal =oaCOrderPersonalMapper.selectOne(queryWrapper);
        if(CheckUtil.objIsEmpty(oaCOrderPersonal)){
            throw PangException.create(EErrorCode.infoIsNull);
        }
        if (CheckUtil.strIsNotEmpty(oaCOrderPersonal.getUploadId())) {
            return -1;
        }
        oaCOrderPersonal.setUploadId(fileId);

        int sum = oaCOrderPersonalMapper.updateById(oaCOrderPersonal);
        return sum;
    }

    @Override
    public OaCOrderPersonal selectOaCOrderPersonalByOrderNo(String orderNo) {
        QueryWrapper<OaCOrderPersonal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaCOrderPersonal.ORDER_NO,orderNo);
        OaCOrderPersonal oaCOrderPersonal =oaCOrderPersonalMapper.selectOne(queryWrapper);
        if(CheckUtil.objIsEmpty(oaCOrderPersonal)){
            throw PangException.create(EErrorCode.infoIsNull);
        }
        return oaCOrderPersonal;
    }
}
